import type { WorkerBaseAction } from "./action/worker.action";
import { WorkerMainThreadController } from "./controller/worker.main_thread.controller";
import { IWorkerOptions } from "./worker.core";

/**
 * 主线程Worker
 */
export class MainThreadWorker {
    /**
     * Worker 名称
     */
    public name: string;

    /**
     * 主线程通信控制器
     */
    public controller: WorkerMainThreadController;

    /**
     * 是否已经终止掉 Worker
     */
    protected isTerminated = false;

    public constructor(options: IWorkerOptions) {
        this.name = options.workerName;
        this.controller = new WorkerMainThreadController(options);
    }

    /**
     * 初始化业务actions
     */
    public initActions() {}

    /**
     * 销毁 worker 实例
     * 子实例需要销毁action
     */
    public terminate(): void {
        this.controller.terminate();
        // 设置终止标志位
        this.isTerminated = true;
    }

    /**
     * 实例化action
     * @param Action action类
     */
    protected createAction<T extends typeof WorkerBaseAction>(Action: T): InstanceType<T> {
        return (new Action(this.controller, this)) as InstanceType<T>;
    }
}
